package com.huicharge.huichargecrm.result;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 请求返回的result
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> implements Serializable {

    private T result;

    private int resultCode;

    private String resultMsg;

    private Boolean success;

    public static  <T>Result success(T t){
        return new Result(t);
    }
    public static  Result failure(){
        return new Result(ResultCodes.SYSTEM_EXCEPTION);
    }

    //public Result(){}

    /**
     * 初始化 Result 常用于错误的返回
     *
     * @param resultCode
     */
    public Result(ResultCode resultCode) {
        this.resultCode = resultCode.getResultCode();
        this.resultMsg = resultCode.getResultMsg();
    }

    /**
     * 初始化 Result 常用于正确的返回
     */
    public Result(T result) {
        this.resultCode = ResultCodes.OK.getResultCode();
        this.resultMsg = ResultCodes.OK.getResultMsg();
        this.result = result;
    }

    /**
     * 初始化 Result 常用于正确的返回
     */
/*    public Result(ResultCode resultCode, T result) {
        this.resultCode = resultCode.getResultCode();
        this.resultMsg = resultCode.getResultMsg();
        this.result = result;
    }*/

    public Boolean getSuccess() {
        return ResultCodes.OK.getResultCode()==resultCode;
    }
}
